<template>
  <div class="setting-container">
    <div class="app-container">
      <el-card v-loading="loading">
        <el-tabs v-model="activeName">
          <el-tab-pane label="角色管理" name="role">
            <el-button type="primary" size="smail" @click="showDialog = true">
              新增角色
            </el-button>
            <el-table :data="list">
              <el-table-column
                label="序号"
                type="index"
                :index="indexMethod"
                width="100px"
              />
              <el-table-column label="角色名称" prop="name" />
              <el-table-column label="描述" prop="description" />
              <el-table-column label="操作">
                <template #default="{ row }">
                  <el-button size="small" type="text">分配权限</el-button>
                  <el-button
                    size="small"
                    type="text"
                    @click="showEdit(row.id)"
                  >编辑</el-button>
                  <el-button
                    size="small"
                    type="text"
                    @click="del(row.id)"
                  >删除</el-button>
                </template>
              </el-table-column>
            </el-table>
            <!-- 分页 -->

            <el-pagination
              :current-page="page"
              :page-sizes="[1, 2, 3, 4, 5]"
              :page-size="pagesize"
              layout="total, sizes, prev, pager, next, jumper"
              :total="total"
              @size-change="handleSizeChange"
              @current-change="handleCurrentChange"
            />
          </el-tab-pane>
          <el-tab-pane label="公司内容" name="company">
            <el-alert
              title="对公司名称、公司地址、营业执照、公司地区的更新，将使得公司资料被重新审核，请谨慎修改"
              type="info"
              show-icon
              :closable="false"
            />
            <!-- 表单 -->
            <el-form label-width="120px" style="margin-top: 50px">
              <el-form-item label="公司名称">
                <el-input
                  v-model="companyForm.name"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="公司地址">
                <el-input
                  v-model="companyForm.companyAddress"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="邮箱">
                <el-input
                  v-model="companyForm.mailbox"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
              <el-form-item label="备注">
                <el-input
                  v-model="companyForm.remarks"
                  type="textarea"
                  :rows="1"
                  disabled
                  style="width: 400px"
                />
              </el-form-item>
            </el-form>
          </el-tab-pane>
        </el-tabs>
      </el-card>
      <!-- 新增角色 -->
      <el-dialog :title="showTitle" :visible="showDialog" @close="closeDialog">
        <el-form ref="form" :model="form" :rules="rules" label-width="100px">
          <el-form-item label="角色名称" prop="name">
            <el-input v-model="form.name" placeholder="请输入角色名称" />
          </el-form-item>
          <el-form-item label="角色描述" prop="description">
            <el-input v-model="form.description" placeholder="请输入角色描述" />
          </el-form-item>
        </el-form>
        <template #footer>
          <el-button @click="showDialog = false">取消</el-button>
          <el-button type="primary" @click="clickSubmit">确定</el-button>
        </template>
      </el-dialog>
    </div>
  </div>
</template>

<script>
import {
  reqGetRoleList,
  reqDeleteRole,
  reqAddRole,
  reqGetRoleDetail,
  reqUpdateRole,
  reqGetCompanyById
} from '@/api/setting'
import { mapActions } from 'vuex'
export default {
  name: 'Setting',
  data() {
    return {
      activeName: 'role',
      list: [],
      companyForm: [],
      page: 1,
      pagesize: 5,
      total: 0,
      loading: false,
      showDialog: false,
      form: {
        name: '',
        description: ''
      },
      rules: {
        name: [
          {
            required: true,
            message: '请输入角色名称',
            trigger: ['blur', 'change']
          }
        ],
        description: [
          {
            required: true,
            message: '请输入角色描述',
            trigger: ['blur', 'change']
          }
        ]
      }
    }
  },
  computed: {
    ...mapActions('user', ['userInfo']),
    showTitle() {
      return this.form.id ? '编辑角色' : '添加角色'
    }
  },
  created() {
    this.getRoleList()
    this.getCompanyInfo()
  },
  methods: {
    // 公司设置
    async getCompanyInfo() {
      const { data } = await reqGetCompanyById(
        this.$store.state.user.userInfo.companyId
      )
      this.companyForm = data
      console.log(this.companyForm)
    },
    // 渲染
    async getRoleList() {
      this.loading = true
      const {
        data: { total, rows }
      } = await reqGetRoleList(this.page, this.pagesize)
      this.total = total
      this.list = rows
      this.loading = false
    },
    // 删除
    del(id) {
      this.$confirm('确认删除该角色吗?', '温馨提示')
        .then(async() => {
          await reqDeleteRole(id)
          if (this.list.length === 1 && this.page > 1) {
            this.page--
          }
          this.getRoleList()
          this.$message.success('删除成功')
        })
        .catch(() => {
          this.$message.error('取消')
        })
    },
    // 新增 编辑
    clickSubmit() {
      this.$refs.form.validate(async(flag) => {
        if (!flag) return
        if (this.form.id) {
          await reqUpdateRole(this.form)
          this.$message.success('编辑成功')
        } else {
          await reqAddRole(this.form)
          this.$message.success('添加成功')
        }

        // 关闭弹框

        // 重新渲染
        this.getRoleList()
        // 添加成功
        this.showDialog = false
      })
    },
    async showEdit(id) {
      const { data } = await reqGetRoleDetail(id)
      this.form = data
      this.showDialog = true
      // console.log(res)
    },
    closeDialog() {
      this.showDialog = false
      this.$refs.form.resetFields()
      this.form = {
        name: '',
        description: ''
      }
    },
    handleSizeChange(val) {
      this.pagesize = val
      this.page = 1
      this.getRoleList()
    },
    handleCurrentChange(val) {
      this.page = val
      this.getRoleList()
    },
    indexMethod(index) {
      return index + 1 + this.pagesize * (this.page - 1)
    }
  }
}
</script>

<style></style>
